rsubmit;
data delist; set crsp.mse
(keep = cusip date event dlret dlstcd);
where event="DELIST" and dlret<10 and 1961<year(date)<2018;
year=year(date); month=month(date); drop date;
data crsp; set crsp.msf
(keep = cusip date ret prc shrout);
where 1961<year(date)<2018;
year=year(date); month=month(date);
me=abs(prc)*shrout; price=abs(prc);
drop date prc shrout;
proc sort data=delist; by cusip year month;
proc sort data=crsp; by cusip year month;
data crsp; merge crsp (in=in1) delist (in=in2);
by cusip year month; if in1;
proc means; run;

data exch; set crsp.mse (keep = date cusip exchcd shrcd);
year=year(date); month=month(date); drop date;
proc sort data=exch; by cusip year month;
data crsp; merge crsp exch; by cusip year month;
data temp.crsp; set crsp; by cusip year month;
i=0; do while(i<2); lexchcd = lag(exchcd); lshrcd=lag(shrcd);
if cusip = lag(cusip) and exchcd = . then exchcd = lexchcd;
if cusip = lag(cusip) and shrcd = . then shrcd = lshrcd;
i+1; end; if last.month then output;
drop i lexchcd lshrcd;
proc sort data=temp.crsp nodupkey;
by cusip year month;
data temp.crsp; set temp.crsp;
if (nmiss(dlret)=1 or dlret=0) and 500<=dlstcd<600 and (exchcd ne 0 
and exchcd ne 3 and exchcd ne 33) then dlret=-0.3;
if (nmiss(dlret)=1 or dlret=0) and 500<=dlstcd<600 and
(exchcd=0 or exchcd=3 or exchcd=33) then dlret=-0.55;
if nmiss(ret)=1 and nmiss(dlret)=0 then return=dlret*100;
else return=ret*100; if shrcd ne 10 and shrcd ne 11 then delete;
drop event ret dlret dlstcd shrcd;
proc means; run;

rsubmit;
data data1; set comp.co_afnd1 (keep = gvkey datadate
dltt ceq csho dlc);	proc sort; by gvkey datadate;
data data2; set comp.co_afnd2 (keep = gvkey datadate
txdb); proc sort; by gvkey datadate;
data price; set comp.co_amkt (keep = gvkey datadate
cfflag prcc); where cfflag='F'; proc sort; by gvkey datadate;
data year; set comp.co_adesind (keep = gvkey datadate
fyear fyr);	proc sort; by gvkey datadate;
data sic; set comp.company (keep = gvkey sic); proc sort; by gvkey;
data cusip; set comp.security (keep = gvkey cusip); proc sort; by gvkey;
data compann; merge data1 data2 year price; by gvkey datadate;
data temp.compann; merge compann sic cusip; by gvkey;
if (4900<=sic<5000 or 6000<=sic<7000) then delete;
year=fyear; if 0<fyr<5 then year=year+1;
if year<1961 or year>2017 then delete;
tdebt=dltt+dlc; mve=csho*prcc; mlev=tdebt/(tdebt+mve);
if tdebt+mve<=0 then mlev=.; if nmiss(txdb) then data74=0;
cusip=substr(cusip,1,8); bvalue=ceq+txdb; mb=mve/bvalue;
if mb<=0 then mb=.; if nmiss(mlev) and nmiss(mb) then delete;
keep cusip year mlev mb; proc sort data=temp.compann nodupkey;
by cusip year; proc means; run;

rsubmit;
data temp.daily; set crsp.dsf (keep = cusip date ret);
where 1962<year(date)<2018; proc sort; by date; run;
data index; set ff.factors_daily
(keep = date mktrf rf smb hml); proc sort; by date;
data temp.daily; merge temp.daily (in=in1) index;
by date; if in1; eret=(1+ret)/(1+rf)-1;
yearm=(year(date)-1963)*12+month(date);
proc sort; by cusip yearm; run;
proc means data=temp.daily; run;

rsubmit;
options nosource nonotes errors=0;
proc reg data=temp.daily outest=temp.best noprint;
model eret = mktrf smb hml / edf; by cusip yearm;
data temp.betas17; set temp.best;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12;
beta=mktrf; s=smb; h=hml;
if _edf_+_p_<15 then delete;
keep cusip year month beta s h;
proc means; run;

rsubmit;
options source notes errors=5;
data crsp; set temp.crsp (keep = cusip year month return);
proc sort data=crsp; by cusip year month;
data ivol; merge crsp temp.ivol; by cusip year month;
data ivol; merge temp.betas17 ivol; by cusip year month;
data char; set temp.compann; year=year+1;
proc sort data=char; by cusip year;
data ivol; merge char ivol; by cusip year;
data cap; set temp.crsp (keep = cusip year month me exchcd price);
where month=12; year=year+1; lme=log(me);
drop month; proc sort; by cusip year;
data ivol; merge ivol cap; by cusip year;
if year<1963 or year>2017 then delete;
if freq<15 then ivol=.; drop alpha s h freq;
proc means; run;

proc sort data=ivol; by cusip descending year descending month;
data ivol; set ivol; freturn=lag(return);
lcusip=lag(cusip); betacapm=lag(beta);
if lcusip ne cusip then betacapm=.;
if lcusip ne cusip then freturn=.;
proc sort data=ivol; by year month;
proc rank data=ivol out=ranked percent; by year month;
var ivol mb me; ranks rivol rmb rme;
data ranked; set ranked; freturn=freturn*100; rimb=rivol*rmb/100;
proc sort data=ranked nodupkey; by cusip year month;
proc means; run;

rsubmit;
proc sort data=ranked; by year month;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = betacapm rme rmb; by year month;
proc tabulate data=est format=9.6;
var betacapm rme rmb _rsq_ _adjrsq_;
table betacapm rme rmb _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=betacapm; size1=rme; mb1=rmb;
keep year month beta1 size1 mb1; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = betacapm rme rmb rivol; by year month;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol _rsq_ _adjrsq_;
table betacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=betacapm; size2=rme; mb2=rmb; ivol2=rivol;
keep year month beta2 size2 mb2 ivol2; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = betacapm rme rmb rimb; by year month;
proc tabulate data=est format=9.6;
var betacapm rme rmb rimb _rsq_ _adjrsq_;
table betacapm rme rmb rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=betacapm; size3=rme; mb3=rmb; ivolmb3=rimb;
keep year month beta3 size3 mb3 ivolmb3; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = betacapm rme rmb rivol rimb; by year month;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table betacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=betacapm; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4; run;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="csreg"; RUN;

rsubmit;
proc sort data=ranked; by year month;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = betacapm rme rmb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb _rsq_ _adjrsq_;
table betacapm rme rmb _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=betacapm; size1=rme; mb1=rmb;
keep year month beta1 size1 mb1; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = betacapm rme rmb rivol; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol _rsq_ _adjrsq_;
table betacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=betacapm; size2=rme; mb2=rmb; ivol2=rivol;
keep year month beta2 size2 mb2 ivol2; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = betacapm rme rmb rimb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rimb _rsq_ _adjrsq_;
table betacapm rme rmb rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=betacapm; size3=rme; mb3=rmb; ivolmb3=rimb;
keep year month beta3 size3 mb3 ivolmb3; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = betacapm rme rmb rivol rimb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table betacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=betacapm; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4; run;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="csreg5"; RUN;

rsubmit;
proc sort data=ranked; by year month;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb; by year month;
proc tabulate data=est format=9.6;
var beta rme rmb _rsq_ _adjrsq_;
table beta rme rmb _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=beta; size1=rme; mb1=rmb;
keep year month beta1 size1 mb1; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rivol; by year month;
proc tabulate data=est format=9.6;
var beta rme rmb rivol _rsq_ _adjrsq_;
table beta rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=beta; size2=rme; mb2=rmb; ivol2=rivol;
keep year month beta2 size2 mb2 ivol2; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rimb; by year month;
proc tabulate data=est format=9.6;
var beta rme rmb rimb _rsq_ _adjrsq_;
table beta rme rmb rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=beta; size3=rme; mb3=rmb; ivolmb3=rimb;
keep year month beta3 size3 mb3 ivolmb3; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rivol rimb; by year month;
proc tabulate data=est format=9.6;
var beta rme rmb rivol rimb _rsq_ _adjrsq_;
table beta rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=beta; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4; run;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="csreglag"; RUN;

rsubmit;
proc sort data=ranked; by year month;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var beta rme rmb _rsq_ _adjrsq_;
table beta rme rmb _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=beta; size1=rme; mb1=rmb;
keep year month beta1 size1 mb1; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rivol; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var beta rme rmb rivol _rsq_ _adjrsq_;
table beta rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=beta; size2=rme; mb2=rmb; ivol2=rivol;
keep year month beta2 size2 mb2 ivol2; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rimb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var beta rme rmb rimb _rsq_ _adjrsq_;
table beta rme rmb rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=beta; size3=rme; mb3=rmb; ivolmb3=rimb;
keep year month beta3 size3 mb3 ivolmb3; run;
proc reg data=ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rivol rimb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var beta rme rmb rivol rimb _rsq_ _adjrsq_;
table beta rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=beta; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4; run;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="csreglag5"; RUN;

rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
proc sort data=crsp; by year month;
data factors; set ff.factors_monthly
(keep = year month mktrf rf); proc sort; by year month;
data crsp; merge crsp (in=in1) factors;
by year month; if in1 and year>1961 and year<2018;
yearm=(year-1963)*12+month;	mkt=mktrf*100;
eret=((1+return/100)/(1+rf)-1)*100;
drop return mktrf year month rf; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=25; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-60)
and yearm le %eval(&myear-1); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=temp.ivol&myear noprint;
model eret = mkt / edf; by cusip;
data temp.ivol&myear; set temp.ivol&myear;
if _edf_+_p_<24 then delete; yearm = %eval(&myear);
beta=mkt; keep cusip yearm beta; run; %end;
%mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=25; %let endyear=660;
%macro makebeta(st); data temp.betacapm17; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.betacapm17; set temp.betacapm17;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
proc sort data=crsp; by year month;
proc upload data=sasuser.fvixmon17
out=temp.fvixmon (replace=yes);
data crsp; merge crsp (in=in1) temp.fvixmon;
by year month; if in1 and year>1985 and year<2018;
yearm=(year-1963)*12+month; eret=((1+return/100)/(1+rf/100)-1)*100;
keep cusip yearm eret mkt fvix fivol dvix; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=313; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-36)
and yearm le %eval(&myear-1); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.fvix
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=temp.ivol&myear noprint;
model eret = mkt fvix / edf; by cusip;
data temp.ivol&myear; set temp.ivol&myear;
if _edf_+_p_<24 then delete; yearm = %eval(&myear);
beta=mkt; keep cusip yearm beta fvix;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=313; %let endyear=660;
%macro makebeta(st); data temp.betafvix17; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.betafvix17; set temp.betafvix17;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=313; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-36)
and yearm le %eval(&myear-1); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.fvix, sub.fivol
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=temp.ivol&myear noprint;
model eret = mkt fvix fivol / edf; by cusip;
data temp.ivol&myear; set temp.ivol&myear;
if _edf_+_p_<24 then delete; yearm = %eval(&myear);
beta=mkt; keep cusip yearm beta fvix fivol;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=313; %let endyear=660;
%macro makebeta(st); data temp.betafivol; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.betafivol; set temp.betafivol;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
options source notes errors=5;
data crsp; set temp.crsp (keep = cusip year month return);
proc sort data=crsp; by cusip year month;
data ivol; merge crsp temp.ivol; by cusip year month;
data beta; set temp.betacapm17; betacapm=beta; drop beta;
data betafivol; set temp.betafivol; beta3=beta; fvix3=fvix; drop beta fvix;
data ivol; merge temp.betafvix17 beta ivol betafivol; by cusip year month;
data char; set temp.compann; year=year+1; proc sort; by cusip year;
data ivol; merge char ivol; by cusip year;
data cap; set temp.crsp (keep = cusip year month me price);
where month=12; year=year+1; lme=log(me);
drop month; proc sort; by cusip year;
data ivol; merge ivol cap; by cusip year;
if year<1963 or year>2017 then delete;
if freq<15 then ivol=.;

proc sort data=ivol; by cusip descending year descending month;
data ivol; set ivol; freturn=lag(return); fbetacapm=lag2(betacapm); fbeta=lag2(beta);
ffivol=lag2(fivol); ffvix=lag2(fvix); ffvix3=lag2(fvix3); fbeta3=lag2(beta3);
if lag2(cusip) ne cusip then betacapm=.; if lag2(cusip) ne cusip then ffivol=.;
if lag(cusip) ne cusip then freturn=.; if lag2(cusip) ne cusip then betacapm=.;
if lag2(cusip) ne cusip then ffvix=.; if lag2(cusip) ne cusip then ffvix3=.;
if lag2(cusip) ne cusip then beta3=.; proc sort data=ivol; by year month;
proc rank data=ivol out=ranked percent; by year month;
var ivol mb me fvix fvix3 fivol ffvix ffvix3 ffivol;
ranks rivol rmb rme rfvix rfvix3 rfivol rffvix rffvix3 rffivol;
data temp.ranked; set ranked; freturn=freturn*100; rimb=rivol*rmb/100;
proc sort data=temp.ranked nodupkey; by cusip year month;
proc means; run;

rsubmit;
proc sort data=temp.ranked; by year month;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbetacapm rme rmb rivol; by year month; where year>1988;
proc tabulate data=est format=9.6;
var fbetacapm rme rmb rivol _rsq_ _adjrsq_;
table fbetacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=fbetacapm; size1=rme; mb1=rmb;
ivol1=rivol; keep year month beta1 size1 mb1 ivol1;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbeta rme rmb rivol rffvix; by year month; where year>1988;
proc tabulate data=est format=9.6;
var fbeta rme rmb rivol rffvix _rsq_ _adjrsq_;
table fbeta rme rmb rivol rffvix _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=fbeta; size2=rme; mb2=rmb; ivol2=rivol;
fvix2=rffvix; keep year month beta2 size2 mb2 ivol2 fvix2; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbeta3 rme rmb rivol rffvix3 rffivol; by year month; where year>1988;
proc tabulate data=est format=9.6;
var fbeta3 rme rmb rivol rffvix3 rffivol _rsq_ _adjrsq_;
table fbeta3 rme rmb rivol rffvix3 rffivol _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=fbeta3; size3=rme; mb3=rmb; ivol3=rivol; fvix3=rffvix3;
fivol3=rffivol; keep year month beta3 size3 mb3 ivol3 fvix3 fivol3; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbetacapm rme rmb rivol rimb; by year month; where year>1988;
proc tabulate data=est format=9.6;
var fbetacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table fbetacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=fbetacapm; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbeta rme rmb rivol rimb rffvix; by year month; where year>1988;
proc tabulate data=est format=9.6;
var fbeta rme rmb rivol rimb rffvix _rsq_ _adjrsq_;
table fbeta rme rmb rivol rimb rffvix _rsq_ _adjrsq_, n mean std t; run;
data est5; set est; beta5=fbeta; size5=rme; mb5=rmb; ivol5=rivol; ivolmb5=rimb;
fvix5=rffvix; keep year month beta5 size5 mb5 ivol5 ivolmb5 fvix5;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbeta3 rme rmb rivol rimb rffvix rffivol; by year month; where year>1988;
proc tabulate data=est format=9.6;
var fbeta3 rme rmb rivol rimb rffvix rffivol _rsq_ _adjrsq_;
table fbeta3 rme rmb rivol rimb rffvix rffivol _rsq_ _adjrsq_, n mean std t; run;
data est6; set est; beta6=fbeta3; size6=rme; mb6=rmb; ivol6=rivol; ivolmb6=rimb;
fvix6=rffvix; fivol6=rffivol; keep year month beta6 size6 mb6 ivol6 ivolmb6 fvix6 fivol6;
data coefs; merge est1 est2 est3 est4 est5 est6; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="fvixcsreg"; RUN;

rsubmit;
proc sort data=temp.ranked; by year month;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbetacapm rme rmb rivol; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var fbetacapm rme rmb rivol _rsq_ _adjrsq_;
table fbetacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=fbetacapm; size1=rme; mb1=rmb;
ivol1=rivol; keep year month beta1 size1 mb1 ivol1;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbeta rme rmb rivol rffvix; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var fbeta rme rmb rivol rffvix _rsq_ _adjrsq_;
table fbeta rme rmb rivol rffvix _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=fbeta; size2=rme; mb2=rmb; ivol2=rivol;
fvix2=rffvix; keep year month beta2 size2 mb2 ivol2 fvix2; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbeta3 rme rmb rivol rffvix3 rffivol; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var fbeta3 rme rmb rivol rffvix3 rffivol _rsq_ _adjrsq_;
table fbeta3 rme rmb rivol rffvix3 rffivol _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=fbeta3; size3=rme; mb3=rmb; ivol3=rivol; fvix3=rffvix3;
fivol3=rffivol; keep year month beta3 size3 mb3 ivol3 fvix3 fivol3; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbetacapm rme rmb rivol rimb; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var fbetacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table fbetacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=fbetacapm; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbeta rme rmb rivol rimb rffvix; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var fbeta rme rmb rivol rimb rffvix _rsq_ _adjrsq_;
table fbeta rme rmb rivol rimb rffvix _rsq_ _adjrsq_, n mean std t; run;
data est5; set est; beta5=fbeta; size5=rme; mb5=rmb; ivol5=rivol; ivolmb5=rimb;
fvix5=rffvix; keep year month beta5 size5 mb5 ivol5 ivolmb5 fvix5;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbeta3 rme rmb rivol rimb rffvix rffivol; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var fbeta3 rme rmb rivol rimb rffvix rffivol _rsq_ _adjrsq_;
table fbeta3 rme rmb rivol rimb rffvix rffivol _rsq_ _adjrsq_, n mean std t; run;
data est6; set est; beta6=fbeta3; size6=rme; mb6=rmb; ivol6=rivol; ivolmb6=rimb;
fvix6=rffvix; fivol6=rffivol; keep year month beta6 size6 mb6 ivol6 ivolmb6 fvix6 fivol6;
data coefs; merge est1 est2 est3 est4 est5 est6; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="fvixcsreg5"; RUN;

rsubmit;
proc sort data=temp.ranked; by year month;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model fvix = betacapm rme rmb; by year month; where year>1988;
proc tabulate data=est format=9.6;
var betacapm rme rmb _rsq_ _adjrsq_;
table betacapm rme rmb _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=betacapm; size1=rme; mb1=rmb;
keep year month beta1 size1 mb1; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model fvix = betacapm rme rmb rivol; by year month; where year>1988;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol _rsq_ _adjrsq_;
table betacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=betacapm; size2=rme; mb2=rmb; ivol2=rivol;
keep year month beta2 size2 mb2 ivol2; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model fvix = betacapm rme rmb rimb; by year month; where year>1988;
proc tabulate data=est format=9.6;
var betacapm rme rmb rimb _rsq_ _adjrsq_;
table betacapm rme rmb rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=betacapm; size3=rme; mb3=rmb; ivolmb3=rimb;
keep year month beta3 size3 mb3 ivolmb3; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model fvix = betacapm rme rmb rivol rimb; by year month; where year>1988;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table betacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=betacapm; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4; run;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="fvixdrivers"; RUN;

rsubmit;
proc sort data=temp.ranked; by year month;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model fvix = betacapm rme rmb; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb _rsq_ _adjrsq_;
table betacapm rme rmb _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=betacapm; size1=rme; mb1=rmb;
keep year month beta1 size1 mb1; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model fvix = betacapm rme rmb rivol; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol _rsq_ _adjrsq_;
table betacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=betacapm; size2=rme; mb2=rmb; ivol2=rivol;
keep year month beta2 size2 mb2 ivol2; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model fvix = betacapm rme rmb rimb; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rimb _rsq_ _adjrsq_;
table betacapm rme rmb rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=betacapm; size3=rme; mb3=rmb; ivolmb3=rimb;
keep year month beta3 size3 mb3 ivolmb3; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model fvix = betacapm rme rmb rivol rimb; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table betacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=betacapm; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4; run;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="fvixdrivers5"; RUN;

rsubmit;
proc surveyreg data=temp.ranked; cluster cusip;
model fvix = betacapm rme rmb / covb ;
ods output covb=firm FitStatistics=fit ParameterEstimates=var1;
data var1; set var1; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model fvix = betacapm rme rmb /covb ;
ods output covb=time;
proc reg data=temp.ranked;
model fvix = betacapm rme rmb /hcc  acov  covb;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb;
data firm; set firm;
keep intercept betacapm rme rmb;
data time; set time;
keep intercept betacapm rme rmb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar1; set resultsfit; drop n;

data slopevar1; set var1 (keep = parameter estimate);
parvar1=parameter; estvar1=estimate; drop parameter estimate;
data tstatvar1; set var1 (keep = parameter tvalue);
parvar1=parameter; estvar1=tvalue; drop parameter tvalue;
data tablevar1; set slopevar1 tstatvar1; proc sort; by parvar1;
data tablevar1; set tablevar1; if parvar1=lag(parvar1)
then parvar1='tstat';

data slopetwovar1; set twovar1 (keep = parameter estimate);
partwovar1=parameter; esttwovar1=estimate; drop parameter estimate;
data tstattwovar1; set twovar1 (keep = parameter tvalue);
partwovar1=parameter; esttwovar1=tvalue; drop parameter tvalue;
data tabletwovar1; set slopetwovar1 tstattwovar1;
if partwovar1='R-square' then delete; proc sort; by partwovar1;
data tabletwovar1; set tabletwovar1; if partwovar1=lag(partwovar1)
then partwovar1='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model fvix = betacapm rme rmb rivol / covb ;
ods output covb=firm FitStatistics=fit ParameterEstimates=var2;
data var2; set var2; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model fvix = betacapm rme rmb rivol /covb ;
ods output covb=time;
proc reg data=temp.ranked;
model fvix = betacapm rme rmb rivol /hcc  acov  covb;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rivol;
data firm; set firm;
keep intercept betacapm rme rmb rivol;
data time; set time;
keep intercept betacapm rme rmb rivol;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar2; set resultsfit; drop n;

data slopevar2; set var2 (keep = parameter estimate);
parvar2=parameter; estvar2=estimate; drop parameter estimate;
data tstatvar2; set var2 (keep = parameter tvalue);
parvar2=parameter; estvar2=tvalue; drop parameter tvalue;
data tablevar2; set slopevar2 tstatvar2; proc sort; by parvar2;
data tablevar2; set tablevar2; if parvar2=lag(parvar2)
then parvar2='tstat';

data slopetwovar2; set twovar2 (keep = parameter estimate);
partwovar2=parameter; esttwovar2=estimate; drop parameter estimate;
data tstattwovar2; set twovar2 (keep = parameter tvalue);
partwovar2=parameter; esttwovar2=tvalue; drop parameter tvalue;
data tabletwovar2; set slopetwovar2 tstattwovar2;
if partwovar2='R-square' then delete; proc sort; by partwovar2;
data tabletwovar2; set tabletwovar2; if partwovar2=lag(partwovar2)
then partwovar2='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model fvix = betacapm rme rmb rimb / covb ;
ods output covb=firm FitStatistics=fit ParameterEstimates=var3;
data var3; set var3; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model fvix = betacapm rme rmb rimb /covb ;
ods output covb=time;
proc reg data=temp.ranked;
model fvix = betacapm rme rmb rimb /hcc  acov  covb;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rimb;
data firm; set firm;
keep intercept betacapm rme rmb rimb;
data time; set time;
keep intercept betacapm rme rmb rimb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar3; set resultsfit; drop n;

data slopevar3; set var3 (keep = parameter estimate);
parvar3=parameter; estvar3=estimate; drop parameter estimate;
data tstatvar3; set var3 (keep = parameter tvalue);
parvar3=parameter; estvar3=tvalue; drop parameter tvalue;
data tablevar3; set slopevar3 tstatvar3; proc sort; by parvar3;
data tablevar3; set tablevar3; if parvar3=lag(parvar3)
then parvar3='tstat';

data slopetwovar3; set twovar3 (keep = parameter estimate);
partwovar3=parameter; esttwovar3=estimate; drop parameter estimate;
data tstattwovar3; set twovar3 (keep = parameter tvalue);
partwovar3=parameter; esttwovar3=tvalue; drop parameter tvalue;
data tabletwovar3; set slopetwovar3 tstattwovar3;
if partwovar3='R-square' then delete; proc sort; by partwovar3;
data tabletwovar3; set tabletwovar3; if partwovar3=lag(partwovar3)
then partwovar3='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model fvix = betacapm rme rmb rivol rimb / covb ;
ods output covb=firm FitStatistics=fit ParameterEstimates=var4;
data var4; set var4; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model fvix = betacapm rme rmb rivol rimb /covb ;
ods output covb=time;
proc reg data=temp.ranked;
model fvix = betacapm rme rmb rivol rimb /hcc  acov  covb;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rivol rimb;
data firm; set firm;
keep intercept betacapm rme rmb rivol rimb;
data time; set time;
keep intercept betacapm rme rmb rivol rimb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar4; set resultsfit; drop n;

data slopevar4; set var4 (keep = parameter estimate);
parvar4=parameter; estvar4=estimate; drop parameter estimate;
data tstatvar4; set var4 (keep = parameter tvalue);
parvar4=parameter; estvar4=tvalue; drop parameter tvalue;
data tablevar4; set slopevar4 tstatvar4; proc sort; by parvar4;
data tablevar4; set tablevar4; if parvar4=lag(parvar4)
then parvar4='tstat';

data slopetwovar4; set twovar4 (keep = parameter estimate);
partwovar4=parameter; esttwovar4=estimate; drop parameter estimate;
data tstattwovar4; set twovar4 (keep = parameter tvalue);
partwovar4=parameter; esttwovar4=tvalue; drop parameter tvalue;
data tabletwovar4; set slopetwovar4 tstattwovar4;
if partwovar4='R-square' then delete; proc sort; by partwovar4;
data tabletwovar4; set tabletwovar4; if partwovar4=lag(partwovar4)
then partwovar4='tstat';

data table2; merge tabletwovar1 tabletwovar2 tabletwovar3 tabletwovar4; proc print;
data table; merge tablevar1 tablevar2 tablevar3 tablevar4; proc print; run;
proc download data=table out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=XLS REPLACE; SHEET="fvix_pet_firm"; RUN;
rsubmit;
proc download data=table2
out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=XLS REPLACE; SHEET="fvix_pet_2way"; RUN;

rsubmit;
proc surveyreg data=temp.ranked; cluster cusip;
model fvix = betacapm rme rmb / covb; where price ge 5;
ods output covb=firm FitStatistics=fit ParameterEstimates=var1;
data var1; set var1; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model fvix = betacapm rme rmb /covb; where price ge 5;
ods output covb=time;
proc reg data=temp.ranked;
model fvix = betacapm rme rmb /hcc  acov  covb; where price ge 5;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb;
data firm; set firm;
keep intercept betacapm rme rmb;
data time; set time;
keep intercept betacapm rme rmb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar1; set resultsfit; drop n;

data slopevar1; set var1 (keep = parameter estimate);
parvar1=parameter; estvar1=estimate; drop parameter estimate;
data tstatvar1; set var1 (keep = parameter tvalue);
parvar1=parameter; estvar1=tvalue; drop parameter tvalue;
data tablevar1; set slopevar1 tstatvar1; proc sort; by parvar1;
data tablevar1; set tablevar1; if parvar1=lag(parvar1)
then parvar1='tstat';

data slopetwovar1; set twovar1 (keep = parameter estimate);
partwovar1=parameter; esttwovar1=estimate; drop parameter estimate;
data tstattwovar1; set twovar1 (keep = parameter tvalue);
partwovar1=parameter; esttwovar1=tvalue; drop parameter tvalue;
data tabletwovar1; set slopetwovar1 tstattwovar1;
if partwovar1='R-square' then delete; proc sort; by partwovar1;
data tabletwovar1; set tabletwovar1; if partwovar1=lag(partwovar1)
then partwovar1='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model fvix = betacapm rme rmb rivol / covb; where price ge 5;
ods output covb=firm FitStatistics=fit ParameterEstimates=var2;
data var2; set var2; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model fvix = betacapm rme rmb rivol /covb; where price ge 5;
ods output covb=time;
proc reg data=temp.ranked;
model fvix = betacapm rme rmb rivol /hcc  acov  covb; where price ge 5;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rivol;
data firm; set firm;
keep intercept betacapm rme rmb rivol;
data time; set time;
keep intercept betacapm rme rmb rivol;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar2; set resultsfit; drop n;

data slopevar2; set var2 (keep = parameter estimate);
parvar2=parameter; estvar2=estimate; drop parameter estimate;
data tstatvar2; set var2 (keep = parameter tvalue);
parvar2=parameter; estvar2=tvalue; drop parameter tvalue;
data tablevar2; set slopevar2 tstatvar2; proc sort; by parvar2;
data tablevar2; set tablevar2; if parvar2=lag(parvar2)
then parvar2='tstat';

data slopetwovar2; set twovar2 (keep = parameter estimate);
partwovar2=parameter; esttwovar2=estimate; drop parameter estimate;
data tstattwovar2; set twovar2 (keep = parameter tvalue);
partwovar2=parameter; esttwovar2=tvalue; drop parameter tvalue;
data tabletwovar2; set slopetwovar2 tstattwovar2;
if partwovar2='R-square' then delete; proc sort; by partwovar2;
data tabletwovar2; set tabletwovar2; if partwovar2=lag(partwovar2)
then partwovar2='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model fvix = betacapm rme rmb rimb / covb; where price ge 5;
ods output covb=firm FitStatistics=fit ParameterEstimates=var3;
data var3; set var3; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model fvix = betacapm rme rmb rimb /covb; where price ge 5;
ods output covb=time;
proc reg data=temp.ranked;
model fvix = betacapm rme rmb rimb /hcc  acov  covb; where price ge 5;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rimb;
data firm; set firm;
keep intercept betacapm rme rmb rimb;
data time; set time;
keep intercept betacapm rme rmb rimb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar3; set resultsfit; drop n;

data slopevar3; set var3 (keep = parameter estimate);
parvar3=parameter; estvar3=estimate; drop parameter estimate;
data tstatvar3; set var3 (keep = parameter tvalue);
parvar3=parameter; estvar3=tvalue; drop parameter tvalue;
data tablevar3; set slopevar3 tstatvar3; proc sort; by parvar3;
data tablevar3; set tablevar3; if parvar3=lag(parvar3)
then parvar3='tstat';

data slopetwovar3; set twovar3 (keep = parameter estimate);
partwovar3=parameter; esttwovar3=estimate; drop parameter estimate;
data tstattwovar3; set twovar3 (keep = parameter tvalue);
partwovar3=parameter; esttwovar3=tvalue; drop parameter tvalue;
data tabletwovar3; set slopetwovar3 tstattwovar3;
if partwovar3='R-square' then delete; proc sort; by partwovar3;
data tabletwovar3; set tabletwovar3; if partwovar3=lag(partwovar3)
then partwovar3='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model fvix = betacapm rme rmb rivol rimb / covb; where price ge 5;
ods output covb=firm FitStatistics=fit ParameterEstimates=var4;
data var4; set var4; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model fvix = betacapm rme rmb rivol rimb /covb; where price ge 5;
ods output covb=time;
proc reg data=temp.ranked;
model fvix = betacapm rme rmb rivol rimb /hcc  acov  covb; where price ge 5;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rivol rimb;
data firm; set firm;
keep intercept betacapm rme rmb rivol rimb;
data time; set time;
keep intercept betacapm rme rmb rivol rimb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar4; set resultsfit; drop n;

data slopevar4; set var4 (keep = parameter estimate);
parvar4=parameter; estvar4=estimate; drop parameter estimate;
data tstatvar4; set var4 (keep = parameter tvalue);
parvar4=parameter; estvar4=tvalue; drop parameter tvalue;
data tablevar4; set slopevar4 tstatvar4; proc sort; by parvar4;
data tablevar4; set tablevar4; if parvar4=lag(parvar4)
then parvar4='tstat';

data slopetwovar4; set twovar4 (keep = parameter estimate);
partwovar4=parameter; esttwovar4=estimate; drop parameter estimate;
data tstattwovar4; set twovar4 (keep = parameter tvalue);
partwovar4=parameter; esttwovar4=tvalue; drop parameter tvalue;
data tabletwovar4; set slopetwovar4 tstattwovar4;
if partwovar4='R-square' then delete; proc sort; by partwovar4;
data tabletwovar4; set tabletwovar4; if partwovar4=lag(partwovar4)
then partwovar4='tstat';

data table2; merge tabletwovar1 tabletwovar2 tabletwovar3 tabletwovar4; proc print;
data table; merge tablevar1 tablevar2 tablevar3 tablevar4; proc print; run;
proc download data=table out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=XLS REPLACE; SHEET="fvix5_pet_firm"; RUN;
rsubmit;
proc download data=table2
out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=XLS REPLACE; SHEET="fvix5_pet_2way"; RUN;

rsubmit;
options nosource nonotes errors=0;
%let begyear=313; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-36)
and yearm le %eval(&myear-1); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.dvix
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=temp.ivol&myear noprint;
model eret = mkt dvix / edf; by cusip;
data temp.ivol&myear; set temp.ivol&myear;
if _edf_+_p_<24 then delete; yearm = %eval(&myear);
beta=mkt; keep cusip yearm beta dvix;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=313; %let endyear=660;
%macro makebeta(st); data temp.dvix17; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.dvix17; set temp.dvix17;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data temp.daily; set crsp.dsf (keep = cusip date ret);
where 1985<year(date)<2018; year=year(date); month=month(date);
day=day(date); drop date; proc sort; by year month day; run;

rsubmit;
proc upload data=sasuser.fvix17
out=temp.fvix17 (replace=yes);
data temp.fvix17; set temp.fvix17;
if vix500=-99 then vix500=.;
if dvix500=-99 then dvix500=.;
drop date; proc sort; by year month day;
data index; set ff.factors_daily (keep = date mktrf rf);
mkt=mktrf*100; drop mktrf; year=year(date); month=month(date);
day=day(date); drop date; proc sort; by year month day;
data temp.daily; merge temp.daily (in=in1) temp.fvix17 index;
by year month day; if in1; eret=((1+ret)/(1+rf)-1)*100;
drop rf ret; proc sort; by cusip year month; proc means; run;

rsubmit;
options nosource nonotes errors=0;
proc reg data=temp.daily outest=temp.best noprint;
model eret = mkt dvix / edf; by cusip year month;
data temp.vixnew17; set temp.best;
vix=dvix; if _edf_+_p_<15 then delete;
keep cusip year month vix; proc means; run;

rsubmit;
options source notes errors=5;
data crsp; set temp.crsp (keep = cusip year month return);
proc sort data=crsp; by cusip year month;
data ivol; merge crsp temp.ivol; by cusip year month;
data beta; set temp.betas17; betacapm=beta; drop s h;
data ivol; merge ivol beta temp.dvix17 temp.vixnew17; by cusip year month;
data char; set temp.compann; year=year+1; proc sort; by cusip year;
data ivol; merge char ivol; by cusip year;
data cap; set temp.crsp (keep = cusip year month me price);
where month=12; year=year+1; lme=log(me);
drop month; proc sort; by cusip year;
data ivol; merge ivol cap; by cusip year;
if year<1963 or year>2017 then delete;
if freq<15 then ivol=.; drop freq;

proc sort data=ivol; by cusip descending year descending month;
data ivol; set ivol; freturn=lag(return); fbetacapm=lag(betacapm);
fbeta=lag(beta); fvix=lag(vix); fdvix=lag2(dvix);
if lag(cusip) ne cusip then fbetacapm=.; if lag(cusip) ne cusip then fvix=.;
if lag(cusip) ne cusip then freturn=.; if lag2(cusip) ne cusip then fdvix=.;
if lag(cusip) ne cusip then fbeta=.; proc sort data=ivol; by year month;
proc rank data=ivol out=ranked percent; by year month;
var ivol mb me vix dvix fvix fdvix;
ranks rivol rmb rme rvix rdvix rfvix rfdvix;
data temp.ranked; set ranked; freturn=freturn*100; rimb=rivol*rmb/100;
proc sort data=temp.ranked nodupkey; by cusip year month;
proc means; run;

rsubmit;
proc sort data=temp.ranked; by year month;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbetacapm rme rmb rivol; by year month; where year>1985;
proc tabulate data=est format=9.6;
var fbetacapm rme rmb rivol _rsq_ _adjrsq_;
table fbetacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=fbetacapm; size1=rme; mb1=rmb;
ivol1=rivol; keep year month beta1 size1 mb1 ivol1;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rivol rvix; by year month; where year>1988;
proc tabulate data=est format=9.6;
var beta rme rmb rivol rvix _rsq_ _adjrsq_;
table beta rme rmb rivol rvix _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=beta; size2=rme; mb2=rmb; ivol2=rivol;
dvix2=rvix; keep year month beta2 size2 mb2 ivol2 dvix2; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbetacapm rme rmb rivol rimb; by year month; where year>1985;
proc tabulate data=est format=9.6;
var fbetacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table fbetacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=fbetacapm; size3=rme; mb3=rmb; ivol3=rivol;
ivolmb3=rimb; keep year month beta3 size3 mb3 ivol3 ivolmb3;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rivol rimb rvix; by year month; where year>1988;
proc tabulate data=est format=9.6;
var beta rme rmb rivol rimb rvix _rsq_ _adjrsq_;
table beta rme rmb rivol rimb rvix _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=beta; size4=rme; mb4=rmb; ivol4=rivol; ivolmb4=rimb;
dvix4=rvix; keep year month beta4 size4 mb4 ivol4 ivolmb4 dvix4;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dvixcsreg"; RUN;

rsubmit;
proc sort data=temp.ranked; by year month;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbetacapm rme rmb rivol; by year month; where year>1985 and price ge 5;
proc tabulate data=est format=9.6;
var fbetacapm rme rmb rivol _rsq_ _adjrsq_;
table fbetacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=fbetacapm; size1=rme; mb1=rmb;
ivol1=rivol; keep year month beta1 size1 mb1 ivol1;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rivol rvix; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var beta rme rmb rivol rvix _rsq_ _adjrsq_;
table beta rme rmb rivol rvix _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=beta; size2=rme; mb2=rmb; ivol2=rivol;
dvix2=rvix; keep year month beta2 size2 mb2 ivol2 dvix2; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = fbetacapm rme rmb rivol rimb; by year month; where year>1985 and price ge 5;
proc tabulate data=est format=9.6;
var fbetacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table fbetacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=fbetacapm; size3=rme; mb3=rmb; ivol3=rivol;
ivolmb3=rimb; keep year month beta3 size3 mb3 ivol3 ivolmb3;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model freturn = beta rme rmb rivol rimb rvix; by year month; where year>1988 and price ge 5;
proc tabulate data=est format=9.6;
var beta rme rmb rivol rimb rvix _rsq_ _adjrsq_;
table beta rme rmb rivol rimb rvix _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=beta; size4=rme; mb4=rmb; ivol4=rivol; ivolmb4=rimb;
dvix4=rvix; keep year month beta4 size4 mb4 ivol4 ivolmb4 dvix4;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dvixcsreg5"; RUN;

rsubmit;
proc sort data=temp.ranked; by year month;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model dvix = betacapm rme rmb; by year month;
proc tabulate data=est format=9.6;
var betacapm rme rmb _rsq_ _adjrsq_;
table betacapm rme rmb _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=betacapm; size1=rme; mb1=rmb;
keep year month beta1 size1 mb1; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model dvix = betacapm rme rmb rivol; by year month;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol _rsq_ _adjrsq_;
table betacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=betacapm; size2=rme; mb2=rmb; ivol2=rivol;
keep year month beta2 size2 mb2 ivol2; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model dvix = betacapm rme rmb rimb; by year month;
proc tabulate data=est format=9.6;
var betacapm rme rmb rimb _rsq_ _adjrsq_;
table betacapm rme rmb rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=betacapm; size3=rme; mb3=rmb; ivolmb3=rimb;
keep year month beta3 size3 mb3 ivolmb3; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model dvix = betacapm rme rmb rivol rimb; by year month;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table betacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=betacapm; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4; run;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dvixdrivers"; RUN;

rsubmit;
proc sort data=temp.ranked; by year month;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model dvix = betacapm rme rmb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb _rsq_ _adjrsq_;
table betacapm rme rmb _rsq_ _adjrsq_, n mean std t; run;
data est1; set est; beta1=betacapm; size1=rme; mb1=rmb;
keep year month beta1 size1 mb1; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model dvix = betacapm rme rmb rivol; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol _rsq_ _adjrsq_;
table betacapm rme rmb rivol _rsq_ _adjrsq_, n mean std t; run;
data est2; set est; beta2=betacapm; size2=rme; mb2=rmb; ivol2=rivol;
keep year month beta2 size2 mb2 ivol2; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model dvix = betacapm rme rmb rimb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rimb _rsq_ _adjrsq_;
table betacapm rme rmb rimb _rsq_ _adjrsq_, n mean std t; run;
data est3; set est; beta3=betacapm; size3=rme; mb3=rmb; ivolmb3=rimb;
keep year month beta3 size3 mb3 ivolmb3; run;
proc reg data=temp.ranked outest=est noprint edf adjrsq;
model dvix = betacapm rme rmb rivol rimb; by year month; where price ge 5;
proc tabulate data=est format=9.6;
var betacapm rme rmb rivol rimb _rsq_ _adjrsq_;
table betacapm rme rmb rivol rimb _rsq_ _adjrsq_, n mean std t; run;
data est4; set est; beta4=betacapm; size4=rme; mb4=rmb; ivol4=rivol;
ivolmb4=rimb; keep year month beta4 size4 mb4 ivol4 ivolmb4; run;
data coefs; merge est1 est2 est3 est4; by year month; proc means;
proc download data=coefs out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dvixdrivers5"; RUN;

rsubmit;
proc surveyreg data=temp.ranked; cluster cusip;
model dvix = betacapm rme rmb / covb ;
ods output covb=firm FitStatistics=fit ParameterEstimates=var1;
data var1; set var1; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model dvix = betacapm rme rmb /covb ;
ods output covb=time;
proc reg data=temp.ranked;
model dvix = betacapm rme rmb /hcc  acov  covb;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb;
data firm; set firm;
keep intercept betacapm rme rmb;
data time; set time;
keep intercept betacapm rme rmb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar1; set resultsfit; drop n;

data slopevar1; set var1 (keep = parameter estimate);
parvar1=parameter; estvar1=estimate; drop parameter estimate;
data tstatvar1; set var1 (keep = parameter tvalue);
parvar1=parameter; estvar1=tvalue; drop parameter tvalue;
data tablevar1; set slopevar1 tstatvar1; proc sort; by parvar1;
data tablevar1; set tablevar1; if parvar1=lag(parvar1)
then parvar1='tstat';

data slopetwovar1; set twovar1 (keep = parameter estimate);
partwovar1=parameter; esttwovar1=estimate; drop parameter estimate;
data tstattwovar1; set twovar1 (keep = parameter tvalue);
partwovar1=parameter; esttwovar1=tvalue; drop parameter tvalue;
data tabletwovar1; set slopetwovar1 tstattwovar1;
if partwovar1='R-square' then delete; proc sort; by partwovar1;
data tabletwovar1; set tabletwovar1; if partwovar1=lag(partwovar1)
then partwovar1='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model dvix = betacapm rme rmb rivol / covb ;
ods output covb=firm FitStatistics=fit ParameterEstimates=var2;
data var2; set var2; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model dvix = betacapm rme rmb rivol /covb ;
ods output covb=time;
proc reg data=temp.ranked;
model dvix = betacapm rme rmb rivol /hcc  acov  covb;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rivol;
data firm; set firm;
keep intercept betacapm rme rmb rivol;
data time; set time;
keep intercept betacapm rme rmb rivol;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar2; set resultsfit; drop n;

data slopevar2; set var2 (keep = parameter estimate);
parvar2=parameter; estvar2=estimate; drop parameter estimate;
data tstatvar2; set var2 (keep = parameter tvalue);
parvar2=parameter; estvar2=tvalue; drop parameter tvalue;
data tablevar2; set slopevar2 tstatvar2; proc sort; by parvar2;
data tablevar2; set tablevar2; if parvar2=lag(parvar2)
then parvar2='tstat';

data slopetwovar2; set twovar2 (keep = parameter estimate);
partwovar2=parameter; esttwovar2=estimate; drop parameter estimate;
data tstattwovar2; set twovar2 (keep = parameter tvalue);
partwovar2=parameter; esttwovar2=tvalue; drop parameter tvalue;
data tabletwovar2; set slopetwovar2 tstattwovar2;
if partwovar2='R-square' then delete; proc sort; by partwovar2;
data tabletwovar2; set tabletwovar2; if partwovar2=lag(partwovar2)
then partwovar2='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model dvix = betacapm rme rmb rimb / covb ;
ods output covb=firm FitStatistics=fit ParameterEstimates=var3;
data var3; set var3; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model dvix = betacapm rme rmb rimb /covb ;
ods output covb=time;
proc reg data=temp.ranked;
model dvix = betacapm rme rmb rimb /hcc  acov  covb;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rimb;
data firm; set firm;
keep intercept betacapm rme rmb rimb;
data time; set time;
keep intercept betacapm rme rmb rimb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar3; set resultsfit; drop n;

data slopevar3; set var3 (keep = parameter estimate);
parvar3=parameter; estvar3=estimate; drop parameter estimate;
data tstatvar3; set var3 (keep = parameter tvalue);
parvar3=parameter; estvar3=tvalue; drop parameter tvalue;
data tablevar3; set slopevar3 tstatvar3; proc sort; by parvar3;
data tablevar3; set tablevar3; if parvar3=lag(parvar3)
then parvar3='tstat';

data slopetwovar3; set twovar3 (keep = parameter estimate);
partwovar3=parameter; esttwovar3=estimate; drop parameter estimate;
data tstattwovar3; set twovar3 (keep = parameter tvalue);
partwovar3=parameter; esttwovar3=tvalue; drop parameter tvalue;
data tabletwovar3; set slopetwovar3 tstattwovar3;
if partwovar3='R-square' then delete; proc sort; by partwovar3;
data tabletwovar3; set tabletwovar3; if partwovar3=lag(partwovar3)
then partwovar3='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model dvix = betacapm rme rmb rivol rimb / covb ;
ods output covb=firm FitStatistics=fit ParameterEstimates=var4;
data var4; set var4; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model dvix = betacapm rme rmb rivol rimb /covb ;
ods output covb=time;
proc reg data=temp.ranked;
model dvix = betacapm rme rmb rivol rimb /hcc  acov  covb;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rivol rimb;
data firm; set firm;
keep intercept betacapm rme rmb rivol rimb;
data time; set time;
keep intercept betacapm rme rmb rivol rimb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar4; set resultsfit; drop n;

data slopevar4; set var4 (keep = parameter estimate);
parvar4=parameter; estvar4=estimate; drop parameter estimate;
data tstatvar4; set var4 (keep = parameter tvalue);
parvar4=parameter; estvar4=tvalue; drop parameter tvalue;
data tablevar4; set slopevar4 tstatvar4; proc sort; by parvar4;
data tablevar4; set tablevar4; if parvar4=lag(parvar4)
then parvar4='tstat';

data slopetwovar4; set twovar4 (keep = parameter estimate);
partwovar4=parameter; esttwovar4=estimate; drop parameter estimate;
data tstattwovar4; set twovar4 (keep = parameter tvalue);
partwovar4=parameter; esttwovar4=tvalue; drop parameter tvalue;
data tabletwovar4; set slopetwovar4 tstattwovar4;
if partwovar4='R-square' then delete; proc sort; by partwovar4;
data tabletwovar4; set tabletwovar4; if partwovar4=lag(partwovar4)
then partwovar4='tstat';

data table2; merge tabletwovar1 tabletwovar2 tabletwovar3 tabletwovar4; proc print;
data table; merge tablevar1 tablevar2 tablevar3 tablevar4; proc print; run;
proc download data=table out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=XLS REPLACE; SHEET="dvix_pet_firm"; RUN;
rsubmit;
proc download data=table2
out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=XLS REPLACE; SHEET="dvix_pet_2way"; RUN;

rsubmit;
proc surveyreg data=temp.ranked; cluster cusip;
model dvix = betacapm rme rmb / covb; where price ge 5;
ods output covb=firm FitStatistics=fit ParameterEstimates=var1;
data var1; set var1; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model dvix = betacapm rme rmb /covb; where price ge 5;
ods output covb=time;
proc reg data=temp.ranked;
model dvix = betacapm rme rmb /hcc  acov  covb; where price ge 5;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb;
data firm; set firm;
keep intercept betacapm rme rmb;
data time; set time;
keep intercept betacapm rme rmb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar1; set resultsfit; drop n;

data slopevar1; set var1 (keep = parameter estimate);
parvar1=parameter; estvar1=estimate; drop parameter estimate;
data tstatvar1; set var1 (keep = parameter tvalue);
parvar1=parameter; estvar1=tvalue; drop parameter tvalue;
data tablevar1; set slopevar1 tstatvar1; proc sort; by parvar1;
data tablevar1; set tablevar1; if parvar1=lag(parvar1)
then parvar1='tstat';

data slopetwovar1; set twovar1 (keep = parameter estimate);
partwovar1=parameter; esttwovar1=estimate; drop parameter estimate;
data tstattwovar1; set twovar1 (keep = parameter tvalue);
partwovar1=parameter; esttwovar1=tvalue; drop parameter tvalue;
data tabletwovar1; set slopetwovar1 tstattwovar1;
if partwovar1='R-square' then delete; proc sort; by partwovar1;
data tabletwovar1; set tabletwovar1; if partwovar1=lag(partwovar1)
then partwovar1='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model dvix = betacapm rme rmb rivol / covb; where price ge 5;
ods output covb=firm FitStatistics=fit ParameterEstimates=var2;
data var2; set var2; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model dvix = betacapm rme rmb rivol /covb; where price ge 5;
ods output covb=time;
proc reg data=temp.ranked;
model dvix = betacapm rme rmb rivol /hcc  acov  covb; where price ge 5;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rivol;
data firm; set firm;
keep intercept betacapm rme rmb rivol;
data time; set time;
keep intercept betacapm rme rmb rivol;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar2; set resultsfit; drop n;

data slopevar2; set var2 (keep = parameter estimate);
parvar2=parameter; estvar2=estimate; drop parameter estimate;
data tstatvar2; set var2 (keep = parameter tvalue);
parvar2=parameter; estvar2=tvalue; drop parameter tvalue;
data tablevar2; set slopevar2 tstatvar2; proc sort; by parvar2;
data tablevar2; set tablevar2; if parvar2=lag(parvar2)
then parvar2='tstat';

data slopetwovar2; set twovar2 (keep = parameter estimate);
partwovar2=parameter; esttwovar2=estimate; drop parameter estimate;
data tstattwovar2; set twovar2 (keep = parameter tvalue);
partwovar2=parameter; esttwovar2=tvalue; drop parameter tvalue;
data tabletwovar2; set slopetwovar2 tstattwovar2;
if partwovar2='R-square' then delete; proc sort; by partwovar2;
data tabletwovar2; set tabletwovar2; if partwovar2=lag(partwovar2)
then partwovar2='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model dvix = betacapm rme rmb rimb / covb; where price ge 5;
ods output covb=firm FitStatistics=fit ParameterEstimates=var3;
data var3; set var3; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model dvix = betacapm rme rmb rimb /covb; where price ge 5;
ods output covb=time;
proc reg data=temp.ranked;
model dvix = betacapm rme rmb rimb /hcc  acov  covb; where price ge 5;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rimb;
data firm; set firm;
keep intercept betacapm rme rmb rimb;
data time; set time;
keep intercept betacapm rme rmb rimb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar3; set resultsfit; drop n;

data slopevar3; set var3 (keep = parameter estimate);
parvar3=parameter; estvar3=estimate; drop parameter estimate;
data tstatvar3; set var3 (keep = parameter tvalue);
parvar3=parameter; estvar3=tvalue; drop parameter tvalue;
data tablevar3; set slopevar3 tstatvar3; proc sort; by parvar3;
data tablevar3; set tablevar3; if parvar3=lag(parvar3)
then parvar3='tstat';

data slopetwovar3; set twovar3 (keep = parameter estimate);
partwovar3=parameter; esttwovar3=estimate; drop parameter estimate;
data tstattwovar3; set twovar3 (keep = parameter tvalue);
partwovar3=parameter; esttwovar3=tvalue; drop parameter tvalue;
data tabletwovar3; set slopetwovar3 tstattwovar3;
if partwovar3='R-square' then delete; proc sort; by partwovar3;
data tabletwovar3; set tabletwovar3; if partwovar3=lag(partwovar3)
then partwovar3='tstat';

proc surveyreg data=temp.ranked; cluster cusip;
model dvix = betacapm rme rmb rivol rimb / covb; where price ge 5;
ods output covb=firm FitStatistics=fit ParameterEstimates=var4;
data var4; set var4; drop dendf probt; proc print; run;
proc surveyreg data=temp.ranked; cluster year month;
model dvix = betacapm rme rmb rivol rimb /covb; where price ge 5;
ods output covb=time;
proc reg data=temp.ranked;
model dvix = betacapm rme rmb rivol rimb /hcc  acov  covb; where price ge 5;
ods output acovest=both; ods output parameterestimates=parm; run;

data both; set both; parameter=Variable;
data both; set both; drop variable Dependent Model; 
data parm; set parm; parameter=Variable;
keep parameter estimate; run;

data parm1; set parm;
n=_n_; m=1; keep m n;
data parm1; set parm1;
by m; if last.m; keep n;
data both; set both;
keep intercept betacapm rme rmb rivol rimb;
data firm; set firm;
keep intercept betacapm rme rmb rivol rimb;
data time; set time;
keep intercept betacapm rme rmb rivol rimb;

data fit1; set fit;
parameter=Label1; Estimate=nValue1;
if parameter="R-square" then output;
data fit1; set fit1; n=1;
keep parameter Estimate n;
proc iml; use both;
read all var _num_ into Z;
use firm; read all var _num_ into X;
use time; read all var _num_ into Y;
use parm1; read all var _num_ into n;
B=X+Y-Z; C=I(n); D=J(n,1); E=C#B; F=E*D; G=F##.5;
create b from G [colname='stderr'];
append from G; quit;

data results; merge parm B;
tValue=estimate/stderr; n=0;
data resultsfit; merge results fit1; by n;
data twovar4; set resultsfit; drop n;

data slopevar4; set var4 (keep = parameter estimate);
parvar4=parameter; estvar4=estimate; drop parameter estimate;
data tstatvar4; set var4 (keep = parameter tvalue);
parvar4=parameter; estvar4=tvalue; drop parameter tvalue;
data tablevar4; set slopevar4 tstatvar4; proc sort; by parvar4;
data tablevar4; set tablevar4; if parvar4=lag(parvar4)
then parvar4='tstat';

data slopetwovar4; set twovar4 (keep = parameter estimate);
partwovar4=parameter; esttwovar4=estimate; drop parameter estimate;
data tstattwovar4; set twovar4 (keep = parameter tvalue);
partwovar4=parameter; esttwovar4=tvalue; drop parameter tvalue;
data tabletwovar4; set slopetwovar4 tstattwovar4;
if partwovar4='R-square' then delete; proc sort; by partwovar4;
data tabletwovar4; set tabletwovar4; if partwovar4=lag(partwovar4)
then partwovar4='tstat';

data table2; merge tabletwovar1 tabletwovar2 tabletwovar3 tabletwovar4; proc print;
data table; merge tablevar1 tablevar2 tablevar3 tablevar4; proc print; run;
proc download data=table out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=XLS REPLACE; SHEET="dvix5_pet_firm"; RUN;
rsubmit;
proc download data=table2
out=sasuser.est (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.EST 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=XLS REPLACE; SHEET="dvix5_pet_2way"; RUN;
